\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}

\input{../labStyle}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}

\begin{document}




\license{This is a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported (\web{http://creativecommons.org/licenses/by-sa/3.0/}). This lab was adapted for OpenIntro by Andrew Bray and Mine \c{C}etinkaya-Rundel from a lab written by the faculty and TAs of UCLA Statistics.}

\section*{Lab 7: Introduction to linear regression}

\subsection*{Batter up}

The movie \emph{Moneyball} focuses on the ``quest for the secret of success in baseball''.  It follows a low-budget team, the Oakland Athletics, who believed that underused statistics, such as a player's ability to get on base, better predict the ability to score runs than typical statistics like home runs, RBIs (runs batted in), and batting average.  Obtaining players who excelled in these underused statistics turned out to be much more affordable for the team.

In this lab we'll be looking at data from all 30 Major League Baseball teams and examining the linear relationship between runs scored in a season and a number of other player statistics.  Our aim will be to summarize these relationships both graphically and numerically in order to find which variable, if any, helps us best predict a team's runs scored in a season.

\subsection*{The data}
Let's load up the data for the 2011 season.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{download.file}\hlstd{(}\hlstr{"http://www.openintro.org/stat/data/mlb11.RData"}\hlstd{,} \hlkwc{destfile} \hlstd{=} \hlstr{"mlb11.RData"}\hlstd{)}

\hlkwd{load}\hlstd{(}\hlstr{"mlb11.RData"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}


In addition to runs scored, there are seven traditionally used variables in the data set: at-bats, hits, home runs, batting average, strikeouts, stolen bases, and wins\symbolfootnote[2]{Though it's not necessary for this lab, if you'd like a refresher in the rules of baseball and a description of these statistics, visit \web{http://en.wikipedia.org/wiki/Baseball_rules} and \web{http://en.wikipedia.org/wiki/Baseball_statistics}.}. There are also three newer variables: on-base percentage, slugging percentage, and on-base plus slugging.  For the first portion of the analysis we'll consider the seven traditional variables. At the end of the lab, you'll work with the newer variables on your own.

\begin{exercise}
What type of plot would you use to display the relationship between \hlkwd{runs} and one of the other numerical variables?  Plot this relationship using the variable \hlkwd{at\_bats} as the predictor.  Does the relationship look linear?  If you knew a team's \hlkwd{at\_bats}, would you be comfortable using a linear model to predict the number of runs?
\end{exercise}

If the relationship looks linear, we can quantify the strength of the relationship with the correlation coefficient.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{cor}\hlstd{(mlb11}\hlopt{$}\hlstd{runs, mlb11}\hlopt{$}\hlstd{at_bats)}
\end{alltt}
\end{kframe}
\end{knitrout}


\subsection*{Sum of squared residuals}
Think back to the way that we described the distribution of a single variable. Recall that we discussed characteristics such as center, spread, and shape.  It's also useful to be able to describe the relationship of two numerical variables, such as \hlkwd{runs} and \hlkwd{at\_bats} above.

\begin{exercise}
Looking at your plot from the previous exercise, describe the relationship between these two variables. Make sure to discuss the form, direction, and strength of the relationship as well as any unusual observations.
\end{exercise}

Just as we used the mean and standard deviation to summarize a single variable, we can summarize the relationship between these two variables by finding the line that best follows their association.  Use the following interactive function to select the line that you think does the best job of going through the cloud of points.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot_ss}\hlstd{(}\hlkwc{x} \hlstd{= mlb11}\hlopt{$}\hlstd{at_bats,} \hlkwc{y} \hlstd{= mlb11}\hlopt{$}\hlstd{runs)}
\end{alltt}
\end{kframe}
\end{knitrout}


After running this command, you'll be prompted to click two points on the plot to define a line.  Once you've done that, the line you specified will be shown in black and the residuals in blue.  Note that there are 30 residuals, one for each of the 30 observations. Recall that the residuals are the difference between the observed values and the values predicted by the line:

\[ e_i = y_i - \hat{y}_i \]

The most common way to do linear regression is to select the line that minimizes the sum of squared residuals.  To visualize the squared residuals, you can rerun the plot command and add the argument \hlkwc{showSquares = }\hlnum{TRUE}.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot_ss}\hlstd{(}\hlkwc{x} \hlstd{= mlb11}\hlopt{$}\hlstd{at_bats,} \hlkwc{y} \hlstd{= mlb11}\hlopt{$}\hlstd{runs,} \hlkwc{showSquares} \hlstd{=} \hlnum{TRUE}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}


Note that the output from the \hlkwd{plot\_ss} function provides you with the slope and intercept of your line as well as the sum of squares.

\begin{exercise}
Using \hlkwd{plot\_ss}, choose a line that does a good job of minimizing the sum of squares.  Run the function several times.  What was the smallest sum of squares that you got?  How does it compare to your neighbors?
\end{exercise}

\subsection*{The linear model}

It is rather cumbersome to try to get the correct least squares line, i.e. the line that minimizes the sum of squared residuals, through trial and error. Instead we can use the \hlkwd{lm} function in R to fit the linear model (a.k.a. regression line).

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{m1} \hlkwb{<-} \hlkwd{lm}\hlstd{(runs} \hlopt{\mytilde} \hlstd{at_bats,} \hlkwc{data} \hlstd{= mlb11)}
\end{alltt}
\end{kframe}
\end{knitrout}


The first argument in the function \hlkwd{lm} is a formula that takes the form \hlkwd{y}\hlkwd{\texttildelow~}\hlkwd{x}.  Here it can be read that we want to make a linear model of \hlkwd{runs} as a function of \hlkwd{at\_bats}.  The second argument specifies that R should look in the \hlkwd{mlb11} data frame to find the \hlkwd{runs} and \hlkwd{at\_bats} variables.

The output of \hlkwd{lm} is an object that contains all of the information we need about the linear model that was just fit.  We can access this information using the summary function.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{summary}\hlstd{(m1)}
\end{alltt}
\end{kframe}
\end{knitrout}


Let's consider this output piece by piece.  First, the formula used to describe the model is shown at the top. After the formula you find the five-number summary of the residuals. The ``Coefficients'' table shown next is key; its first column displays the linear model's y-intercept and the coefficient of \hlkwd{at\_bats}. With this table, we can write down the least squares regression line for the linear model:

\[ \hat{y} = -2789.2429 + 0.6305 * atbats \]

One last piece of information we will discuss from the summary output is the Multiple R-squared, or more simply, $R^2$. The $R^2$ value represents the proportion of variability in the response variable that is explained by the explanatory variable. For this model, 37.3\% of the variability in runs is explained by at-bats.

\begin{exercise}
Fit a new model that uses \hlkwd{homeruns} to predict \hlkwd{runs}.  Using the estimates from the R output, write the equation of the regression line. What does the slope tell us in the context of the relationship between success of a team and its home runs?
\end{exercise}


\subsection*{Prediction and prediction errors}

Let's create a scatterplot with the least squares line laid on top.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot}\hlstd{(mlb11}\hlopt{$}\hlstd{runs} \hlopt{\mytilde} \hlstd{mlb11}\hlopt{$}\hlstd{at_bats)}

\hlkwd{abline}\hlstd{(m1)}
\end{alltt}
\end{kframe}
\end{knitrout}


The function \hlkwd{abline} plots a line based on its slope and intercept.  Here, we used a shortcut by providing the model \hlkwd{m1}, which contains both parameter estimates. This line can be used to predict $y$ at any value of $x$.  When predictions are made for values of $x$ that are beyond the range of the observed data, it is referred to as \emph{extrapolation} and is not usually recommended.  However, predictions made within the range of the data are more reliable.  They're also used to compute the residuals.

\begin{exercise}
If a team manager saw the least squares regression line and not the actual data, how many runs would he or she predict for a team with 5,578 at-bats? Is this an overestimate or an underestimate, and by how much? In other words, what is the residual for this prediction?
\end{exercise}

%

\subsection*{Model diagnostics}

To assess whether the linear model is reliable, we need to check for  (1) linearity, (2) nearly normal residuals, and (3) constant variability.

\begin{enumerate}[(1)]
\item Linearity: You already checked if the relationship between runs and at-bats is linear using a scatterplot. We should also verify this condition with a plot of the residuals vs. at-bats.  Recall that any code following a \hlcom{\#} is intended to be a comment that helps understand the code but is ignored by R.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot}\hlstd{(m1}\hlopt{$}\hlstd{residuals} \hlopt{\mytilde} \hlstd{mlb11}\hlopt{$}\hlstd{at_bats)}

\hlkwd{abline}\hlstd{(}\hlkwc{h} \hlstd{=} \hlnum{0}\hlstd{,} \hlkwc{lty} \hlstd{=} \hlnum{3}\hlstd{)}  \hlcom{# adds a horizontal dashed line at y = 0}
\end{alltt}
\end{kframe}
\end{knitrout}


\begin{exercise}
Is there any apparent pattern in the residuals plot? What does this indicate about the linearity of the relationship between runs and at-bats?
\end{exercise}

\item Nearly normal residuals: To check this condition, we can look at a histogram

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{hist}\hlstd{(m1}\hlopt{$}\hlstd{residuals)}
\end{alltt}
\end{kframe}
\end{knitrout}


or a normal probability plot of the residuals.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{qqnorm}\hlstd{(m1}\hlopt{$}\hlstd{residuals)}

\hlkwd{qqline}\hlstd{(m1}\hlopt{$}\hlstd{residuals)}  \hlcom{# adds diagonal line to the normal prob plot}
\end{alltt}
\end{kframe}
\end{knitrout}


\begin{exercise}
Based on the histogram and the normal probability plot, does the nearly normal residuals condition appear to be met?
\end{exercise}

\item Constant variability:

\begin{exercise}
Based on the plot in (1), does the constant variability condition appear to be met? 
\end{exercise}

\end{enumerate}

%

\vspace{2cm}

\subsection*{On Your Own}

\begin{enumerate}
\item Choose another traditional variable from \hlkwd{mlb11} that you think might be a good predictor of \hlkwd{runs}. Produce a scatterplot of the two variables and fit a linear model.  At a glance, does there seem to be a linear relationship?

\item How does this relationship compare to the relationship between \hlkwd{runs} and \hlkwd{at\_bats}? Use the R$^2$ values from the two model summaries to compare.  Does your variable seem to predict \hlkwd{runs} better than \hlkwd{at\_bats}?  How can you tell?

\item Now that you can summarize the linear relationship between two variables, investigate the relationships between \hlkwd{runs} and each of the other five traditional variables. Which variable best predicts \hlkwd{runs}? Support your conclusion using the graphical and numerical methods we've discussed (for the sake of conciseness, only include output for the best variable, not all five).

\item Now examine the three newer variables.  These are the statistics used by the author of \emph{Moneyball} to predict a teams success.  In general, are they more or less effective at predicting runs that the old variables?  Explain using appropriate graphical and numerical evidence.  Of all ten variables we've analyzed, which seems to be the best predictor of \hlkwd{runs}?  Using the limited (or not so limited) information you know about these baseball statistics, does your result make sense?

\item Check the model diagnostics for the regression model with the variable you decided was the best predictor for runs.

\item What concepts from the textbook are covered in this lab? What concepts, if any, are not covered in the textbook? Have you seen these concepts elsewhere, e.g. lecture, discussion section, previous labs, or homework problems? Be specific in your answer.

\end{enumerate}

\end{document}
